(function(window){
	let Pipe = window.Pipe = function() {
		this.x = myGame.c.width					//x坐标
		this.pipeDownImg =  myGame.pipe_down	//上管道
		this.pipeUpImg = myGame.pipe_up			//下管道
		this.pipeWidth = myGame.c.height * 0.12	//管道现粗
		this.pipeHeight = this.pipeDownImg.height/this.pipeDownImg.width * this.pipeWidth	//管道现长
		this.pipeUpHeight = parseInt(this.pipeHeight * Math.random())	//下管道长
		//限制下管道高度区间
		this.pipeUpHeight<=this.pipeHeight*0.2 && (this.pipeUpHeight = this.pipeHeight*0.2)
		this.pipeUpHeight>=myGame.bg.height-this.pipeHeight*0.2-myGame.vSpaceHeight && (this.pipeUpHeight = myGame.bg.height-this.pipeHeight*0.2-myGame.vSpaceHeight)
		//上管道长
		this.pipeDownHeight = myGame.bg.height - this.pipeUpHeight - myGame.vSpaceHeight
		//将自己放到数组
		myGame.pipeArr.push(this)
	}
	
	Pipe.prototype.update = function() {
		this.x -= myGame.pipeSpeed
		//去除已通过管道，保持管道数组长度
		if(this.x <= -this.pipeWidth){
			myGame.pipeArr.splice(myGame.pipeArr.indexOf(this),1)
		}
		
		//碰撞检测
		let isCollision = this.x < myGame.bird.x+myGame.bird.birdWidth && this.x+this.pipeWidth > myGame.bird.x
						&& (this.pipeDownHeight > myGame.bird.y || myGame.bg.height-this.pipeUpHeight < myGame.bird.y+myGame.bird.birdHeight)
		if(isCollision){
			//切换到结束场景
			myGame.sceneManager.curScene = 3
		}
	}
	
	Pipe.prototype.render = function() {		//管道间隙
		//图片剪切9参数：context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height)
		//上管道
		myGame.ctx.drawImage(
			this.pipeDownImg,
			0,	//sx
			(this.pipeHeight - this.pipeDownHeight)/this.pipeWidth * this.pipeDownImg.width,  //sy
			this.pipeDownImg.width,  //swidth
			this.pipeDownHeight/this.pipeWidth * this.pipeUpImg.width,  //sheight
			this.x,	//x
			0,		//y
			this.pipeWidth,
			this.pipeDownHeight
		)
		
		//下管道
		myGame.ctx.drawImage(
			this.pipeUpImg,
			0,	//sx
			0,  //sy
			this.pipeUpImg.width,  //swidth
			this.pipeUpHeight/this.pipeWidth * this.pipeUpImg.width,  //sheight
			this.x,	//x
			myGame.bg.height - this.pipeUpHeight,		//y
			this.pipeWidth,
			this.pipeUpHeight
		)
	}
})(window)